package com.wechart.controller;


import com.wechart.dao.domain.Systemlog;
import com.wechart.service.log.SystemlogService;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by silenceholic on 2017/5/8.
 */
@Controller
@RequestMapping("log/")
public class LogController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(LogController.class);

    @Resource(name = "systemlogService")
    private SystemlogService systemlogService;

//    @RequestMapping(value = "systemlog.htm")
//    public String allSystemlog(){
//        return "system/systemlog";
//    }

    /**
     * 系统日志
     * @param start
     * @param end
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "systemlog.htm" )
    public ModelAndView getSystemlog(@RequestParam(value = "logtype", required = false) String logtype,
                                     @RequestParam(value = "start", required = false) String start,
                                     @RequestParam(value = "end", required = false) String end){
        Map<String, Object> map = new HashMap<String, Object>();
        Date end_date = new Date(System.currentTimeMillis()) ;
        Date start_date =  DateUtils.addDays(end_date, -3) ;
        List<Systemlog> data = systemlogService.getSystemlog(start_date,end_date,logtype);
        map.put("data",data);
        map.put("recordsTotal", data.size());
        map.put("recordsFiltered", data.size());
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("log/systemlog");
        return mv;

    }
}
